package com.woniu108.car.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu108.carService.commons.log.AdminHistoryLog;
import com.woniu108.carService.commos.dto.CarDisplacementDto;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.woniu108.car.form.UpdateCarDisplacementForm;
import com.woniu108.car.model.CarDisplacement;
import com.woniu108.car.param.DeleteCarDisplacementParam;
import com.woniu108.car.param.InsertCarDisplacementParam;
import com.woniu108.car.param.UpdateCarDisplacementParam;
import com.woniu108.car.service.CarDisplacementService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 车排量表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/carDisplacement")
@Api(tags = "汽车排量管理")
public class CarDisplacementController {
    @Resource
    private CarDisplacementService carDisplacementService;
    @Resource
    private RedisUtil redisUtil;

    /**
     *  查询所有排量
     * @return 排量
     */
    @GetMapping("/findAllCarDisplacement.c")
    @ApiOperation("查询所有排量")
    public Result<List<CarDisplacementDto>> findAllCarDisplacement() {
        List<CarDisplacement> list = carDisplacementService.list();
        List<CarDisplacementDto> dtos = BeanUtil.copyToList(list, CarDisplacementDto.class);
        return Result.newListInstance(CarDisplacementDto.class).setData(dtos).setMsg("查询热度榜所有未删除品牌信息成功");
    }
    /**
     * 新增排量
     * @param displacementNum 排量
     * @return boolean值
     */
    @GetMapping("/insertCarDisplacement.api")
    @ApiOperation("新增排量")@AdminHistoryLog
    @ApiImplicitParam(name = "displacementNum",value = "排量数")
    public Result<Boolean> insertCarDisplacement(BigDecimal displacementNum, @RequestHeader String token){
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        InsertCarDisplacementParam param=new InsertCarDisplacementParam(displacementNum,adminId);
        Boolean bool= carDisplacementService.insertCarDisplacement(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("新增排量成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("新增排量失败").setData(false).setCode(500);

    }
    /**
     * 修改排量
     * @param form id， 排量
     * @return boolean值
     */
    @PostMapping("/updateCarDisplacement.api")
    @ApiOperation("修改排量")@AdminHistoryLog
    public Result<Boolean> updateCarDisplacement(@RequestBody UpdateCarDisplacementForm form, @RequestHeader String token){
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        UpdateCarDisplacementParam param = BeanUtil.toBean(form, UpdateCarDisplacementParam.class);
        param.setAdminId(adminId);
        Boolean bool= carDisplacementService.updateCarDisplacement(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("修改排量成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("修改排量失败").setData(false).setCode(500);

    }
    /**
     * 删除排量
     * @param  disId 管理员id
     * @return boolean值
     */
    @GetMapping("/deleteCarDisplacement.api")
    @ApiOperation("删除排量")
    @ApiImplicitParam(name = "disId",value = "排量id")
    @AdminHistoryLog
    public Result<Boolean> deleteCarDisplacement(Integer disId, @RequestHeader String token){
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        DeleteCarDisplacementParam param =new DeleteCarDisplacementParam(disId,adminId);
        Boolean bool= carDisplacementService.deleteCarDisplacement(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("删除排量成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("删除排量失败").setData(false).setCode(500);

    }
}

